package com.cirkana.viewer.route.model.gpx;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;

import com.cirkana.serial.SerializablePassData;
import com.cirkana.serial.SerializationException;

public class Tracker extends SerializablePassData {

	private String fileName;
	private Metadata metadata;
	private ArrayList<WayPoint> wayPoints;
	private ArrayList<Track> tracks;

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setMetadata(Metadata metadata) {
		this.metadata = metadata;
	}

	public Metadata getMetadata() {
		return metadata;
	}

	public ArrayList<WayPoint> getWayPoints() {
		return this.wayPoints;
	}

	public void setWayPoints(ArrayList<WayPoint> wayPoints) {
		this.wayPoints = wayPoints;
	}

	public ArrayList<Track> getTracks() {
		return tracks;
	}

	public void setTracks(ArrayList<Track> tracks) {
		this.tracks = tracks;
	}

	// TODO: Should be protected
	public Tracker() {
	}

	@Override
	public void serialize(DataOutputStream out) throws SerializationException {
		serializeString(this.fileName, out);
		serializeSerializable(this.metadata, out);
		serializeSerializableList(this.wayPoints, WayPoint.class, out);
		serializeSerializableList(this.tracks, Track.class, out);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void deserialize(DataInputStream in) throws SerializationException {
		this.fileName = deserializeString(in);
		this.metadata = (Metadata) deserializeSerializable(Metadata.class, in);
		this.wayPoints = deserializeSerializableList(WayPoint.class, in);
		this.tracks = deserializeSerializableList(Track.class, in);
	}

	@Override
	public String toString() {
		StringBuilder s = new StringBuilder("{");
		s.append("fileName=");
		if (this.fileName == null)
			s.append("(null)");
		else
			s.append(this.fileName);
		s.append(",metadata=");
		if (this.metadata == null)
			s.append("(null)");
		else
			s.append(metadata.toString());
		s.append(",wayPoints=");
		if (this.wayPoints == null)
			s.append("(null)");
		else {
			s.append("{");
			for (WayPoint wayPoint : this.wayPoints) {
				if (wayPoint == null)
					s.append("(null)");
				else
					s.append(wayPoint.toString());
				s.append(",");
			}
			s.delete(s.length() - 1, s.length());
			s.append("}");
		}
		s.append(",tracks=");
		if (this.tracks == null)
			s.append("(null)");
		else {
			s.append("{");
			for (Track track : this.tracks) {
				if (track == null)
					s.append("(null)");
				else
					s.append(track.toString());
				s.append(",");
			}
			s.delete(s.length() - 1, s.length());
			s.append("}");
		}

		s.append("}");
		return s.toString();
	}

}
